package com.example.hjt_java_web_homework2.application.service;

import com.example.hjt_java_web_homework2.application.dto.AirConditionerDTO;
import com.example.hjt_java_web_homework2.application.dto.EnvironmentDataDTO;
import com.example.hjt_java_web_homework2.domain.model.AirConditioner;
import com.example.hjt_java_web_homework2.domain.model.EnvironmentData;
import com.example.hjt_java_web_homework2.domain.repository.AirConditionerRepository;
import com.example.hjt_java_web_homework2.domain.repository.EnvironmentDataRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Li Chun Hei
 * @date 31/10/2025
 * @description
 */

@Service
@Transactional
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private AirConditionerRepository airConditionerRepository;

    @Autowired
    private EnvironmentDataRepository environmentDataRepository;

    @Override
    public AirConditionerDTO createAirConditioner(AirConditionerDTO dto) {
        if (airConditionerRepository.existsByDeviceId(dto.getDeviceId())) {
            throw new RuntimeException("设备ID已存在: " + dto.getDeviceId());
        }

        AirConditioner ac = new AirConditioner(dto.getName(), dto.getLocation(), dto.getDeviceId());
        ac.setStatus(dto.getStatus());
        if (dto.getTemperature() != null) ac.setTemperature(dto.getTemperature());
        if (dto.getMode() != null) ac.setMode(dto.getMode());
        if (dto.getFanSpeed() != null) ac.setFanSpeed(dto.getFanSpeed());

        AirConditioner saved = airConditionerRepository.save(ac);
        return convertToAirConditionerDTO(saved);
    }

    @Override
    public AirConditionerDTO updateAirConditioner(Long id, AirConditionerDTO dto) {
        AirConditioner ac = airConditionerRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("空调设备不存在: " + id));

        ac.setName(dto.getName());
        ac.setLocation(dto.getLocation());
        ac.setStatus(dto.getStatus());
        if (dto.getTemperature() != null) ac.setTemperature(dto.getTemperature());
        if (dto.getMode() != null) ac.setMode(dto.getMode());
        if (dto.getFanSpeed() != null) ac.setFanSpeed(dto.getFanSpeed());

        AirConditioner updated = airConditionerRepository.save(ac);
        return convertToAirConditionerDTO(updated);
    }

    @Override
    @Transactional(readOnly = true)
    public AirConditionerDTO getAirConditionerById(Long id) {
        AirConditioner ac = airConditionerRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("空调设备不存在: " + id));
        return convertToAirConditionerDTO(ac);
    }

    @Override
    @Transactional(readOnly = true)
    public List<AirConditionerDTO> getAllAirConditioners() {
        return airConditionerRepository.findAll().stream()
                .map(this::convertToAirConditionerDTO)
                .collect(Collectors.toList());
    }

    @Override
    public void deleteAirConditioner(Long id) {
        if (!airConditionerRepository.findById(id).isPresent()) {
            throw new RuntimeException("空调设备不存在: " + id);
        }
        airConditionerRepository.deleteById(id);
    }

    // 电表管理方法类似，为简洁起见省略详细实现
//    @Override
//    public ElectricMeterDTO createElectricMeter(ElectricMeterDTO dto) {
//        // 实现逻辑类似空调
//        return null;
//    }

    @Override
    public EnvironmentDataDTO recordEnvironmentData(EnvironmentDataDTO dto) {
        EnvironmentData data = new EnvironmentData(
                dto.getTemperature(),
                dto.getHumidity(),
                dto.getEnergyConsumption(),
                dto.getLocation()
        );

        EnvironmentData saved = environmentDataRepository.save(data);
        return convertToEnvironmentDataDTO(saved);
    }

    @Override
    @Transactional(readOnly = true)
    public List<EnvironmentDataDTO> getEnvironmentDataByLocation(String location) {
        return environmentDataRepository.findByLocation(location).stream()
                .map(this::convertToEnvironmentDataDTO)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(readOnly = true)
    public List<EnvironmentDataDTO> getAllEnvironmentData() {
        return environmentDataRepository.findAll().stream()
                .map(this::convertToEnvironmentDataDTO)
                .collect(Collectors.toList());
    }

    // 转换方法
    private AirConditionerDTO convertToAirConditionerDTO(AirConditioner ac) {
        AirConditionerDTO dto = new AirConditionerDTO();
        dto.setId(ac.getId());
        dto.setName(ac.getName());
        dto.setLocation(ac.getLocation());
        dto.setDeviceId(ac.getDeviceId());
        dto.setStatus(ac.getStatus());
        dto.setTemperature(ac.getTemperature());
        dto.setMode(ac.getMode());
        dto.setFanSpeed(ac.getFanSpeed());
        return dto;
    }

    private EnvironmentDataDTO convertToEnvironmentDataDTO(EnvironmentData data) {
        EnvironmentDataDTO dto = new EnvironmentDataDTO();
        dto.setId(data.getId());
        dto.setTemperature(data.getTemperature());
        dto.setHumidity(data.getHumidity());
        dto.setEnergyConsumption(data.getEnergyConsumption());
        dto.setLocation(data.getLocation());
        dto.setRecordedAt(data.getRecordedAt());
        return dto;
    }}
